﻿@page "/auth"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject NotificationService NotificationService
@inject IRBACPermissionService RBACService

<RadzenText TextStyle="TextStyle.H4">权限管理</RadzenText>
<PageTitle>权限管理</PageTitle>
<RadzenStack class="rz-background-color-danger-lighter" Style="padding:5px">
<RadzenStack class="rz-background-color-info" Style="display: flex;" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start" AlignItems="AlignItems.Center">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="权限/编码" />
    <RadzenButton Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:1px" />
    <RadzenButton Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:0px" />
</RadzenStack>
    <RadzenDataGrid EmptyText="暂无数据" TItem="PermissionDto" Data="@auths" GridLines="DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="" MinWidth="20px" MaxWidth="100px" Title="操作" Width="100px" TextAlign="TextAlign.Center">
            <Template Context="data">
                <RadzenStack Gap="5" Orientation="Orientation.Horizontal">
                        <RadzenButton Icon="edit" @onclick:stopPropagation Click=@(args => Update(data)) ButtonStyle="ButtonStyle.Primary" />
                        <RadzenButton Icon="delete" @onclick:stopPropagation Click=@(args => Delete(data)) ButtonStyle="ButtonStyle.Danger" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="UniqueName" Title="名称" />
        <RadzenDataGridColumn Property="UniqueCode" Title="编码" />
    </Columns>
</RadzenDataGrid>
</RadzenStack>
@code {

    string keyword = default!;
    ObservableCollection<PermissionDto> auths = default!;

    private async Task Search() => await QueryAsync();

    private async Task QueryAsync()
    {
        try
        {
            var list = await RBACService.GetListAsync(keyword);
            auths = new ObservableCollection<PermissionDto>(list);
        }
        catch (Exception ex)
        {
            NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = ex.Message });
        }
    }

    private async void Add()
    {
        PermissionDto result = await DialogService.OpenAsync<PermissionAddDialog>(
            $"新增角色", 
            new Dictionary<string, object>() {{ "PPermission", new PermissionDto() }}, 
            new DefaultDialogOptions());
        if (result != null) auths.Add(result);        
    }

    private async void Update(PermissionDto selectedItem)
    {
        PermissionDto result = await DialogService.OpenAsync<PermissionAddDialog>(
            $"修改权限",
            new Dictionary<string, object>() { { "PPermission", selectedItem } },
            new DefaultDialogOptions());

        if (result != null)
        {            
            selectedItem.UniqueName = result.UniqueName;
            selectedItem.UniqueCode = result.UniqueCode;
            StateHasChanged();
        }
    }

    private async void Delete(PermissionDto selectedAuth)
    {
        var result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions() 
        { 
            OkButtonText = "确定", 
            CancelButtonText = "取消" 
        });

        if ((bool)result)
        {
            try
            {
                await RBACService.Delete(selectedAuth.Id);
                auths!.Remove(selectedAuth);                
            }
            catch (Exception ex)
            {                
                NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = ex.Message });
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await QueryAsync();
    }

}
